# -*- python -*-
# Copyright (C) 2003, Charles Wang.
# Author:  Charles Wang <charles@linux.net.cn>
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.

# To install/refresh an language translation, e.g. zh_CN, follow these procedures:
# 1. scons -U newpo
# 2. edit zh_CN.new.po
# 3. mv zh_CN.new.po zh_CN.po

Import('*')

xgettext_opt  = "--default-domain=$textdomain" + \
                " --directory=." + \
                " --add-comments='TO_TRANSLATOR'" + \
                " --keyword=_ --keyword=N_" + \
                " --copyright-holder='$copyright_holder'"

potfiles = []
for f in POTFILES:
    potfiles.append('%s/%s' % (TOPSRCDIR, f))

env.Command('${textdomain}.pot',
            potfiles + ['xmltranslation.py'],
            'xgettext $xgettext_opt -o $TARGET $SOURCES',
            xgettext_opt = xgettext_opt)

potxmls = []
for f in POTXMLS:
    potxmls.append('%s/%s' % (TOPSRCDIR, f))

env.Command('xmltranslation.py', potxmls,
            'python scripts/xmlgettext.py $SOURCES > $TARGET')

for lang in mi_config.all_linguas:
    env['_lang'] = lang
    if not mi_config.lang_map.has_key(lang):
        env.Command('${_lang}.mo',
                    '${_lang}.po',
                    'msgfmt -c -o $TARGET $SOURCE')
    else:
        env.Command('${_lang}.mo',
                    '${_lang}.utf8.po',
                    'msgfmt -c -o $TARGET $SOURCE')
        env.Command('${_lang}.utf8.po',
                    '${_lang}.tmp.po',
                    'sed s/charset=${langcharset}/charset=UTF-8/ $SOURCE > $TARGET',
                    langcharset = mi_config.lang_map[lang][0])
        env.Command('${_lang}.tmp.po',
                    '${_lang}.po',
                    'iconv -f $langcharset -t UTF8 -o $TARGET $SOURCE',
                    langcharset = mi_config.lang_map[lang][1])
    if mi_config.develmode:
        depInstallAs(env, 'root_src',
                     '$destdir/usr/share/locale/${_lang}/LC_MESSAGES/${textdomain}.mo',
                     '${_lang}.mo')

    env.Alias('newpo', '${_lang}.new.po')
    env.Command('${_lang}.new.po',
                ['${_lang}.po',
                    '${textdomain}.pot'],
                ['msgmerge $SOURCES > $TARGET',
                 '@echo "Please replace $SOURCE with edited $TARGET"'])

del env['_lang']
